package com.nhjf.dao.incomeAndExpenditureRecordd;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.nhjf.dao.BaseDao;
import com.nhjf.model.incomeAndExpenditureRecord.IncomeAndExpenditureRecordd;
import com.nhjf.pojo.qo.incomeAndExpenditureRecordd.IncomeAndExpenditureRecorddQo;
@Repository
public class IncomeAndExpenditureRecorddDao extends BaseDao<IncomeAndExpenditureRecordd, IncomeAndExpenditureRecorddQo>{

	@Override
	protected Criteria buildCriteria(Criteria criteria, IncomeAndExpenditureRecorddQo qo) {
		// TODO Auto-generated method stub
		if(qo!=null) {
			criteria.setFetchMode("oranizationAccount", qo.isSeachOranizationAccount()?FetchMode.JOIN:FetchMode.SELECT);	
			criteria.setFetchMode("payLog", qo.isSeachPayLog()?FetchMode.JOIN:FetchMode.SELECT);	
			if(qo.getOranizationAccountId()!=null) {
				criteria.add(Restrictions.eq("oranizationAccount.id",qo.getOranizationAccountId()));
			}
			if(qo.getPayLogId()!=null) {
				criteria.add(Restrictions.eq("payLog.id",qo.getPayLogId()));
			}
			if(StringUtils.isNotBlank(qo.getRemarks())) {
				criteria.add(Restrictions.eq("remarks",qo.getRemarks()));
			}
			if(qo.getChangeAmount()!=null) {
				criteria.add(Restrictions.eq("changeAmount",qo.getChangeAmount()));
			}
			if(qo.getChangeType()!=null) {
				criteria.add(Restrictions.eq("changeType",qo.getChangeType()));
			}
			if(qo.getIncomeAndExpenditureRecorddId()!=null) {
				criteria.add(Restrictions.eq("id",qo.getIncomeAndExpenditureRecorddId()));
			}
			if(qo.getIncomeAndExpenditureType()!=null) {
				criteria.add(Restrictions.eq("incomeAndExpenditureType",qo.getIncomeAndExpenditureType()));
			}
			if(qo.getMyBalanceTime()!=null) {
				criteria.add(Restrictions.le("creatTime",qo.getMyBalanceTime()));
			}
			if(StringUtils.isNotBlank(qo.getOrderNumber())) {
				criteria.createAlias("payLog", "payLog");
				criteria.add(Restrictions.eq("payLog.id",Long.parseLong(qo.getOrderNumber())));
			}
			if(qo.getCreaTime()!=null) {
				criteria.add(Restrictions.ge("creatTime",qo.getCreaTime()));
			}


		}
		return criteria;
	}

	@Override
	protected Class<IncomeAndExpenditureRecordd> getEntityClass() {
		// TODO Auto-generated method stub
		return IncomeAndExpenditureRecordd.class;
	}

}
